<?php
/**
 * 地址管理
 * power bye huanran.zhao
 * QQ: 523994553
 * 2012-05-06
 */
class AddressApp extends CommonApp {

	public function indexAct(){
		//得到与地址关联的对应变量
		$var = M('address')->getRelationVar($_GET);
		$_GET = array_merge($_GET,$var);
		//print_r($var);
		///////////下拉搜索 S
		//全部省
		$select['province'] = M('address')->getProvince();
		//得到市
		if(My::is_id($var['province'])) 	$select['city'] = M('address')->provinceId2city($var['province']);
		//得到区域
		if(My::is_id($var['city']))			$select['area'] = M('address')->cityId2area($var['city']);
		elseif(My::is_id($var['province']))	$select['area'] = M('address')->provinceId2area($var['province']);
		//得到街镇
		if(My::is_id($var['area'])) 		$select['street'] = M('address')->areaId2street($var['area']);
		elseif(My::is_id($var['city']))		$select['street'] = M('address')->cityId2street($var['city']);
		elseif(My::is_id($var['province']))	$select['street'] = M('address')->provinceId2street($var['province']);
		//assign
		$this->assign('select',$select);
		///////////下拉搜索 E

		//////////列表 S
		//省
		$result['province'] = M('address')->getProvince($var['province']);
		if(My::is_id($var['province'])){
			//城市
			$temp = M('address')->getCity($var['city']);
			if(is_array($temp)){
				foreach($temp as &$value){
					$result['city'][$value['province_id']][]=$value;
				}
			}
			unset($temp);
			//区域
			$temp = M('address')->getArea($var['area']);
			if(is_array($temp)){
				foreach($temp as &$value){
					$result['area'][$value['city_id']][]=$value;
				}
			}
			//街镇
			$temp = M('address')->getStreet($var['street']);
			if(is_array($temp)){
				foreach($temp as &$value){
					$result['street'][$value['area_id']][]=$value;
				}
			}
			unset($temp);
		}
		//////////列表 E
		//print_r($result['city']);

		$this->assign('result',$result);
		$this->display();
	}

	/**
	 * select城市onchange
	 */
	public function cityChangeAct(){
		$id = (int)$_REQUEST['id'];
		//区域
		$result['area'] = M('address')->cityId2area($id);
		$html['area'] = "<option value=''>选择区域...</option>";
		if(is_array($result['area']))
		foreach($result['area'] as $key=>$value){
			$html['area'] .= "<option value='{$value['area_id']}'>{$value['area_name']}</option>";
		}
		//街镇
		$result['street'] = M('address')->cityId2street($id);
		$html['street'] = "<option value=''>选择街镇...</option>";
		if(is_array($result['street']))
		foreach($result['street'] as $key=>$value){
			$html['street'] .= "<option value='{$value['street_id']}'>{$value['street_name']}</option>";
		}
		echo json_encode($html);
	}

	/**
	 * select区域onchange
	 */
	public function areaChangeAct(){
		$id = (int)$_REQUEST['id'];
		//街镇
		$result['street'] = M('address')->areaId2street($id);
		$html['street'] = "<option value=''>选择街镇...</option>";
		if(is_array($result['street']))
		foreach($result['street'] as $key=>$value){
			$html['street'] .= "<option value='{$value['street_id']}'>{$value['street_name']}</option>";
		}
		echo json_encode($html);
	}
	
	public function save_orderAct()//保存排序
	{	
		//print_r($_POST);
		//保存省排序
		if( is_array($_POST['id_province']) && count($_POST['id_province'])>0 && count($_POST['order_province'])==count($_POST['id_province']) ){
			foreach($_POST['id_province'] as $key=>$value){
				//echo 'id:'.$value."| order:".$_POST['order_province'][$key]."<br>";
				$this->DB->row_update("province",array('ordering'=>$_POST['order_province'][$key]),"province_id=$value");
			}
		}
		//保存市排序
		if( is_array($_POST['id_city']) && count($_POST['id_city'])>0 && count($_POST['order_city'])==count($_POST['id_city']) ){
			foreach($_POST['id_city'] as $key=>$value){
				//echo 'id:'.$value."| order:".$_POST['order_city'][$key]."<br>";
				$this->DB->row_update("city",array('ordering'=>$_POST['order_city'][$key]),"city_id=$value");
			}
		}
		//保存区排序
		if( is_array($_POST['id_area']) && count($_POST['id_area'])>0 && count($_POST['order_area'])==count($_POST['id_area']) ){
			foreach($_POST['id_area'] as $key=>$value){
				//echo 'id:'.$value."| order:".$_POST['order_area'][$key]."<br>";
				$this->DB->row_update("area",array('ordering'=>$_POST['order_area'][$key]),"area_id=$value");
			}
		}
		//$this->j ump("排序保存成功!",3,urldecode($_POST['url']));
		$this->ajaxReturn(200,"排序保存成功!");
	}
	
	public function modifyAct(){//编辑
		//print_r($_GET);
		$id = My::arr2var($_GET,'id');
		$result = array();
		$parent_id = My::arr2var($_GET,'parent_id');
		
		if(My::arr2var($_GET,'table')=="province" AND My::is_id($id)){
			$result = DB('address_province')->where("`province_id`={$id}")->find();
		}elseif(My::arr2var($_GET,'table')=="city" AND My::is_id($id)){
			$result = DB('address_city')->where("`city_id`={$id}")->find();
		}elseif(My::arr2var($_GET,'table')=="area" AND My::is_id($id)){
			$result = DB('address_area')->where("`area_id`={$id}")->find();
		}elseif(My::arr2var($_GET,'table')=="street" AND My::is_id($id)){
			$result = DB('address_street')->where("`street_id`={$id}")->find();
		}

		$this->assign('publish',array(1=>'是',0=>'否'));
		$this->assign("result",$result);
		$this->display();
	}

	public function saveAct(){
		//print_r($_POST);
		$id = My::arr2var($_POST,'id');
		$table = My::arr2var($_POST,'table');
		if($table!=''){
		    if(!My::is_table('address_'.$table))
		        $this->ajaxReturn(300,"数据表不存在(address_{$table})!");
		    $table_name = I('post.'.$table.'_name');
			if($table_name=='') $this->ajaxReturn(300,'名称不能为空!');
			if(My::is_id($id)) $and="AND {$table}_id<>{$id}"; else $and='';	$num = DB('address_'.$table)->where("{$table}_name='{$table_name}' {$and}")->count();	if($num>0) $this->ajaxReturn(300,'名称已经存在!');
			//更新table
			if(My::is_id($id)){
				$ok = DB('address_'.$table)->where("{$table}_id={$id}")->clear()->save($_POST);
			}else{
				$ok = DB('address_'.$table)->clear()->add($_POST);
			}
			//return
			if($ok) $this->ajaxReturn(200,'操作成功!');
			else 	$this->ajaxReturn(300,'操作失败!');
		}else $this->ajaxReturn(300,'表名不能为空!');
	}

	/**
	 * 删除地址
	 * @param string type 类型(province/city/area/street)
	 * @param int id ID
	 */
	public function deleteAct(){
		$type = My::arr2var($_GET,'type');
		$id = My::arr2var($_GET,'id');
		//filter
		if(!My::is_id($id))
			$this->ajaxReturn(300,'ID不正确!');
		if($type=='province'){
			$temp = M('address')->provinceId2city($id);
			if($temp)
				$this->ajaxReturn(300,'该省下面还有市的数据,不能删除!');
			$ok = DB('address_province')->where("province_id={$id}")->clear()->delete();
			if($ok)
				$this->ajaxReturn(200,'删除成功!');
			else
				$this->ajaxReturn(300,'删除失败!');
		}elseif($type=='city'){
			$temp = M('address')->cityId2area($id);
			if($temp)
				$this->ajaxReturn(300,'该市下面还有区的数据,不能删除!');
			$ok = DB('address_city')->where("city_id={$id}")->clear()->delete();
			if($ok)
				$this->ajaxReturn(200,'删除成功!');
			else
				$this->ajaxReturn(300,'删除失败!');
		}elseif($type=='area'){
			$temp = M('address')->areaId2street($id);
			if($temp)
				$this->ajaxReturn(300,'该区下面还有街镇的数据,不能删除!');
			$ok = DB('address_area')->where("area_id={$id}")->clear()->delete();
			if($ok)
				$this->ajaxReturn(200,'删除成功!');
			else
				$this->ajaxReturn(300,'删除失败!');
		}elseif($type=='street'){
			$ok = DB('address_street')->where("street_id={$id}")->clear()->delete();
			if($ok)
				$this->ajaxReturn(200,'删除成功!');
			else
				$this->ajaxReturn(300,'删除失败!');
		}else{
			$this->ajaxReturn(300,'删除的数据类型不正确!');
		}

	}
	
}